var WastageProcessInfo = function (wastage) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (wastage) {
      var fields = ['deptId', 'deptName', 'workShopId', 'workShopName'];
      self.setDataFromObj(wastage, fields);

      var processName = wastage.equipmentNo;
      if (SysUtil.isEmpty(processName)) {
        processName = '';
      }
      self.processName = processName;

      var energyImg = wastage.equipmentImage;
      if (SysUtil.isEmpty(energyImg)) {
        energyImg = '';
      }
      self.energyImg = energyImg;

      this.setRecords(wastage);
    },

    setRecords: function (wastage) {
      var records = wastage.equipmentRecordPojos;
      var recordObj = {};
      var maxPerEletic = 0;
      var monthCount = 0,
        todayCount = 0,
        monthUseEletic = 0,
        todayUseEletic = 0;
      for (var i = 0; i < records.length; i++) {
        var record = records[i];
        var createTime = record.createTime;
        if (SysUtil.isEmpty(createTime)) {
          createTime = moment().startOf('month').format('YYYY-MM-DD');
          record.createTime = createTime;
        }

        if (moment().isSame(createTime, 'month')) {
          monthUseEletic += record.useEletic;
          monthCount += record.machingCount;
          if (moment().isSame(createTime, 'day')) {
            todayUseEletic = record.useEletic;
            todayCount = record.machingCount;
          }
        }

        var useEletic = record.useEletic;
        useEletic = useEletic * 1000;
        var machingCount = record.machingCount;
        var perEletic = this.getPerEletic(useEletic, machingCount);
        if (maxPerEletic < perEletic) {
          maxPerEletic = perEletic;
        }

        record.perEletic = perEletic;
        recordObj[createTime] = record;
      }

      self.monthCount = monthCount;
      self.todayCount = todayCount;
      self.todayUseEletic = todayUseEletic;
      self.monthUseEletic = monthUseEletic;
      self.maxPerEletic = maxPerEletic;

      var dayMoment = moment().subtract(29, 'day');
      var endMoment = moment();
      var recordData = [];
      while (dayMoment.isSameOrBefore(endMoment, 'day')) {
        var day = dayMoment.format('YYYY-MM-DD');
        var record = recordObj[day];
        if (record === undefined) {
          record = {
            createTime: day,
            machingCount: 0,
            useEletic: 0,
            perEletic: 0
          };
        }
        recordData.push(record);
        dayMoment = dayMoment.add(1, 'day');
      }
      self.recordData = recordData;
    },

    getPerEletic: function (useEletic, count) {
      if (count === 0) {
        return 0;
      }
      var perEletic = useEletic / count;
      perEletic = _.round(perEletic, 2);
      return perEletic;
    }
  };

  this.getRecords = function (dateIndex) {
    var recordData = this.recordData;
    recordData = recordData.slice(dateIndex, dateIndex + 10);
    recordData = _.clone(recordData);
    return recordData;
  };

  fn.initData(wastage);
};
